package com.rjj.wlqq.pdf;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.StringWriter;
import java.util.Map;

public class TemplateToPDFVelocity extends BasePDF{
    private static String path = "templates/pdf/";
    private static VelocityEngine ve = new VelocityEngine();

    static {
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
    }

    @Override
    public String templateToString(Map<String, Object> data, String htmlTmp) {
        Template t = ve.getTemplate(path + htmlTmp);
        VelocityContext ctx = new VelocityContext(data);
        StringWriter sw = new StringWriter();
        t.merge(ctx, sw);
        return sw.toString();
    }
}
